﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.IsolatedStorage;
using System.IO;
using COFE.Core;

namespace COFE.IO.ISG
{
    public class ISGEntryInfo
    {
        #region Constructor

        public ISGEntryInfo(IsolatedStorageFile isolatedStorageFile, string relativePath, bool isFolder)
        {
            StorageFile = isolatedStorageFile;
            RelativePath = relativePath;
            IsFolder = isFolder;
        }

        #endregion

        #region Methods
        public ISGEntryInfo GetParent()
        {
            if (!String.IsNullOrEmpty(RelativePath))
            {
                string parentRelativePath = Path.GetDirectoryName(RelativePath);
                return new ISGEntryInfo(StorageFile, parentRelativePath, true);
            }
            return null;
        }

        public IEnumerable<ISGEntryInfo> EnumerateSubEntries()
        {
            if (!IsFolder)
                throw new NotSupportedException("File cannot list subentries.");

            if (IsExists)
            {
                string mask = String.IsNullOrEmpty(RelativePath) ? "\\*" : RelativePath + "\\*";

                foreach (var dir in StorageFile.GetDirectoryNames(mask))
                    yield return new ISGEntryInfo(StorageFile, Path.Combine(RelativePath, dir), true);

                foreach (var file in StorageFile.GetFileNames(mask))
                    yield return new ISGEntryInfo(StorageFile, Path.Combine(RelativePath, file), false);
            }
            yield break;
        }

        private bool getExists()
        {
            if (String.IsNullOrEmpty(RelativePath)) //Volume
                return true;

            string parentPath = Path.GetDirectoryName(RelativePath);
            string itemName = Path.GetFileName(RelativePath);
            string mask = String.IsNullOrEmpty(parentPath) ? "\\*" : parentPath + "\\*";

            if (IsFolder)
            {
                foreach (var dir in StorageFile.GetDirectoryNames(mask))
                    if (dir.Equals(itemName, StringComparison.InvariantCultureIgnoreCase))
                        return true;
            }
            else
            {
                foreach (var file in StorageFile.GetFileNames(mask))
                    if (file.Equals(itemName, StringComparison.InvariantCultureIgnoreCase))
                        return true;
            }

            return false;
        }

        #endregion


        #region Public Properties

        public IsolatedStorageFile StorageFile { get; private set; }
        public string RelativePath { get; private set; }
        public string Name { get { return Path.GetFileName(RelativePath); } }
        public bool IsFolder { get; private set; }
        public COFE.Core.FileAttributes FileAttributes { get { return IsFolder ? COFE.Core.FileAttributes.Directory : COFE.Core.FileAttributes.Normal; } }
        //public string TempPath { get; set; }
        public bool IsExists { get { return getExists(); } }

        #endregion
    }


}